package com.cjxt.ams.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjxt.ams.dao.RoleDeptDao;
import com.cjxt.ams.domain.RoleDept;
import com.cjxt.ams.service.RoleDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 角色与部门对应关系
 */

@Service("roleDeptService")
public class RoleDeptServiceImpl extends ServiceImpl<RoleDeptDao, RoleDept> implements RoleDeptService {

    @Autowired
    private RoleDeptDao roleDeptDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Integer roleId, List<Integer> deptIdList) {

        /**
         * 先删除角色与部门对应关系
         */
        deleteBatch(new Integer[]{roleId});

        //deptIdList = roleDeptDao.queryDeptIdList(Collections.singletonList(roleId));

        if(deptIdList.size() == 0){
            return;
        }

        for (Integer deptId : deptIdList) {
            RoleDept roleDept = new RoleDept();
            roleDept.setDeptId(deptId);roleDept.setRoleId(roleId);

            this.save(roleDept);

        }

        /**
         * 保存角色与菜单的关系
         */
//        for (RoleDept role : deptIdList){
////            RoleDept roleDept = new RoleDept();
////            roleDept.setDeptId(deptId);
////            roleDept.setRoleId(roleId);
////
////            this.save(roleDept);
//            this.save(role.getDeptId());
//        }
    }


    @Override
    public List<Integer> queryDeptIdList(List<Integer> roleId) {
        return baseMapper.queryDeptIdList(roleId);
    }

    @Override
    public Integer deleteBatch(Integer[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
